﻿using System;
using System.Reflection;
using YiYiTong.Rocket.Helper;

namespace YiYiTong.Rocket.Api
{
    /// <summary>
    /// 基础接口服务类，主要实现方法的注册功能
    /// author：leev
    /// datetime：2018-12-24
    /// </summary>
    public class ApiService
    {
        public ApiService()
        {
            var methods = this.GetType().GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public);

            foreach (var methd in methods)
            {
                try
                {
                    DelegateFactory.DoAction dl = Delegate.CreateDelegate(typeof(DelegateFactory.DoAction), this, methd.Name) as DelegateFactory.DoAction;
                    if (dl != null)
                        DelegateFactory.SetMethod(methd.DeclaringType.Name.Replace("Service", "") + "." + methd.Name, dl);
                }
                catch (ArgumentNullException anex)
                {
                    LogHelper.Error("基础接口服务类异常" + anex.Message);
                }
                catch (ArgumentException aex)
                {
                    LogHelper.Error("基础接口服务类异常" + aex.Message);
                }
            }
        }
    }
}